<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include ("../../include/dbFunctions.php");
include ("../../include/image_slider.php");
include ("../../include/function.php");

if (isset($_SESSION['volunteer_id'])) {
    $volunteer_id = $_SESSION['volunteer_id'];

    $num = 0;
// server validation: check user input
    $array = array('volunteer_firstname', 'volunteer_surname', 'volunteer_email', 'volunteer_password', 'volunteer_mobile', 'volunteer_profession');
    $empty = check_empty($array);
    $mail = check_email('volunteer_email');
    $email = $_POST['volunteer_email'];
    $duplicate_email = check_duplicate('volunteer_email', 'volunteer', $email);

//get input data from volunteerUpdateAccount page
    $volunteer_firstname = htmlspecialchars($_POST['volunteer_firstname'], ENT_QUOTES);
  
    $volunteer_familyname = htmlspecialchars($_POST['volunteer_surname'], ENT_QUOTES);
    $volunteer_salutation = $_POST['volunteer_salutation'];
    $volunteer_gender = $_POST['volunteer_gender'];
    $sponsor_id = $_POST['sponsor_id'];
    $volunteer_profession = htmlspecialchars($_POST['volunteer_profession'], ENT_QUOTES);
    $volunteer_experience = htmlspecialchars($_POST['volunteer_experience'], ENT_QUOTES);
    $volunteer_email = htmlspecialchars($_POST['volunteer_email'], ENT_QUOTES);
    $volunteer_mobile = htmlspecialchars($_POST['volunteer_mobile'], ENT_QUOTES);
    $volunteer_office = $_POST['volunteer_office'];
    $time_id = $_POST['time_id'];

    //$last_modified = date("y-m-j H:i:s ");
    $pass = $_POST['pass'];
    $volunteer_password = $_POST['volunteer_password'];
    if ($volunteer_password == "__________"){
        $volunteer_password = $pass;
    }
    else{
        $volunteer_password = sha1($volunteer_password);
    }
    
    //get volunteer information
    $volunteer = "SELECT * FROM volunteer WHERE volunteer_id = $volunteer_id";
    $volunteer = executeSelectQuery($volunteer);


// store all location into array
    $location = array();
    $loc = "SELECT location_name FROM location";
    $loc = executeSelectQuery($loc);
    for ($i = 0; $i < count($loc); $i++) {
        $location[] = $loc[$i];
    }
//set final location array (business rule: choose no means all)
    if (isset($_POST['location'])) {
        $location_array = $_POST['location'];
    } else {
        $location_array = $location;
    }
//store all location into array
    $education = array();
    $edu = "SELECT education_name FROM education";
    $edu = executeSelectQuery($edu);
    for ($i = 0; $i < count($edu); $i++) {
        $education[] = $edu[$i];
    }
//set final education (business rule: choose no means all)
    if (isset($_POST['education'])) {
        $education_array = $_POST['education'];
    } else {
        $education_array = $education;
    }
    $true = 0;

// update CV   

    function findexts($filename) {
        $filename = strtolower($filename);
        $exts = explode(".", $filename);
        $n = count($exts) - 1;
        $exts = $exts[$n];
        return $exts;
    }

    if ($_FILES['uploadfile']['name'] != '' && $volunteer[0]['volunteer_cv'] != '') {
        $true = 1;
        $tmpfile = "../../admin/upload/cv/" . $volunteer[0]['volunteer_cv'] . "";
        unlink($tmpfile);


        $ext = findexts($_FILES['uploadfile']['name']);
        $ran = rand();
        $ran2 = $ran . ".";
        $target = "../../admin/upload/cv/";
        $target = $target . $ran2 . $ext;

        move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target);
        $volunteer_cv = $ran2 . $ext;
    } elseif ($_FILES['uploadfile']['name'] != '' && $volunteer[0]['volunteer_cv'] == '') {
        $ext = findexts($_FILES['uploadfile']['name']);
        $ran = rand();
        $ran2 = $ran . ".";
        $target = "../../admin/upload/cv/";
        $target = $target . $ran2 . $ext;

        move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target);
        $volunteer_cv = $ran2 . $ext;
    }

// cheking input condition
    if (empty($empty) && empty($mail) && empty($duplicate_emai)) {

        $activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
// Update volunteer information

        if ($_FILES['uploadfile']['name'] != '') {
            $sql = "UPDATE volunteer  SET volunteer_firstname = '$volunteer_firstname',
                                  volunteer_surname = '$volunteer_familyname',
                                  volunteer_salutation = '$volunteer_salutation' ,
                                  volunteer_gender = '$volunteer_gender',
                                  volunteer_profession = '$volunteer_profession',
                                  volunteer_email = '$volunteer_email',
                                  volunteer_mobile = '$volunteer_mobile',
                                  volunteer_office = '$volunteer_office',
                                  volunteer_experience = '$volunteer_experience',
                                  volunteer_password = '$volunteer_password',
                                  sponsor_id = $sponsor_id,
                                  volunteer_cv = '$volunteer_cv'
                                  WHERE volunteer_id = $volunteer_id
                                  ";
            $update = executeQuery($sql);
        } else {
            $sql = "UPDATE volunteer  SET volunteer_firstname = '$volunteer_firstname',
                                  volunteer_surname = '$volunteer_familyname',
                                  volunteer_salutation = '$volunteer_salutation' ,
                                  volunteer_gender = '$volunteer_gender',
                                  volunteer_profession = '$volunteer_profession',
                                  volunteer_email = '$volunteer_email',
                                  volunteer_mobile = '$volunteer_mobile',
                                  volunteer_office = '$volunteer_office',
                                  volunteer_experience = '$volunteer_experience',
                                  volunteer_password = '$volunteer_password',
                                  sponsor_id = $sponsor_id
                                
                                  WHERE volunteer_id = $volunteer_id
                                  ";
            $update = executeQuery($sql);
        }
        // update time
        $delete_time = "DELETE FROM time_preference WHERE volunteer_id = " . $volunteer_id . "";
        $delete_time = executeQuery($delete_time);

        if ($time_id != '2') {
            $insert_time = "INSERT INTO time_preference (volunteer_id, time_id) VALUES ($volunteer_id, '$time_id' )";
            $insert_time = executeQuery($insert_time);
            if ($insert_time == 1) {
                $time = 1;
            }
        } else {
            $insert_time1 = "INSERT INTO time_preference (volunteer_id, time_id) VALUES ($volunteer_id,'0' )";
            $insert_time1 = executeQuery($insert_time1);

            $insert_time2 = "INSERT INTO time_preference (volunteer_id, time_id) VALUES ($volunteer_id,'1' )";
            $insert_time2 = executeQuery($insert_time2);
            if ($insert_time1 == 1 && $insert_time2 == 1) {
                $time = 1;
            }
        }
        if ($time == 1) {
            $total = 0;
            $true = 0;
            $false = 1;
// Delete and insert new location
            $delete_location = "DELETE FROM location_preference WHERE volunteer_id = " . $volunteer_id . "";
            $delete_location = executeQuery($delete_location);
            for ($i = 0; $i < count($location_array); $i++) {

                $location = $location_array[$i];
                $query = "INSERT INTO location_preference (volunteer_id,location_id) VALUES ('" . $volunteer_id . "','" . $location . "')";
                $insertLocation = executeQuery($query);

                if ($insertLocation != 0) {
                    $total = $total + $true;
                } else {
                    $total = $total + $false;
                }
            }
//delete and insert new education level
            $delete_education = "DELETE FROM education_preference WHERE volunteer_id = " . $volunteer_id . "";
            $delete_education = executeQuery($delete_education);

            for ($i = 0; $i < count($education_array); $i++) {
                $education = $education_array[$i];

                $query = "INSERT INTO education_preference (volunteer_id,education_id) VALUES ('" . $volunteer_id . "','" . $education . "')";
                $insertEducation = executeQuery($query);

                if ($insertEducation != 0) {
                    $total = $total + $true;
                } else {
                    $total = $total + $false;
                }
            }

            if ($total > 0) {
                $msg = 'Error:  Problem during insert education  preference. Please try again.<br>';
                $msg.="<input type='button' class='form-back' value='' onclick='history.go(-1)'/>";
            } else {
                $num = 1;
                $msg = "Your record has been updated successfully!<br>";
                $smg = "Please click <a href ='volunteerDetail.php'>Here</a> to go back your detail's site.";
            }
        } else {
            $msg = 'Error: update time slot<br>';
            $msg.="<input type='button' class='form-back' value='' onclick='history.go(-1)'/>";
        }
    } else {
        $msg = 'You forgot to fill all fields, please try again.<br> ';
        $msg.="<input type='button' class='form-back' value='' onclick='history.go(-1)'/>";
    }
}
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Junior Achievement Singapore</title>
        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>

        <!-- Custom jquery scripts -->
        <script src="../../media/js/jquery/custom_jquery.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function(){
            });
        </script>
    </head>
    <body>
        <script>
            var timeout    = 700;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="../../include/home/contact.php">CONTACT</a></li>
                <li><a href="http://ja.org.sg/">ABOUT JA SINGAPORE</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>
            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->
            <div id="featured"><!--FEATURED AREA STARTS-->
                <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"/>
                </div><!--FEATURED IMAGE ENDS-->
                <div class="featured-text"><!--FEATURED TEXT STARTS-->
                    <h2>Welcome to JA Singapore</h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://www.jaworldwide.org/">Read More... </a></p>
                </div><!--FEATURED TEXT ENDS-->
            </div><!--FEATURED AREA ENDS-->
            <?php
            custom_login();
            ?>
            <div id="left-content"><!--RIGHT CONTENT STARTS-->

                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->

                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->
                    <?php
                    if (!isset($_SESSION['volunteer_id'])) {
                        echo "<br/><br/><br/><br/><br/><center><h3>You don't have permission to view this page<h3><br/>
                           Please login to access the page.</center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    } else {
                        echo "<br/><br/><br/><br/><br/><center><p class='pmain'> $msg </p></center>";
                    }
                    if ($num == 1) {
                        ?>
                        <br/><center><input type="button" value="" class="form-back" onClick="window.location.href='volunteerdetail.php'"/></center>
                        <?php
                    }
                    echo "<br/><br/><br/><br/><br/>";
                    ?>
                </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->
                <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
            </div><!--RIGHT CONTENT ENDS-->
        </div><!--CONTAINER ENDS-->
        <div id="footer"><!--FOOTER STARTS-->
            <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
        </div><!--FOOTER ENDS-->
    </body>
</html>

